// pages/shezi/shezi.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    array1: [
      '微信区', 'QQ区'
    ],
    index1: 0,
    game_area: '',
    array2: [],
    index2: 0,
    array3: ['男', '女'],
    index3: 0,
    array4: ['对抗路', '中路', '发育路', '打野', '辅助'],
    index4: 0,
    array5: ['安卓', 'IOS'],
    index5: 0,
    leixing: [],
    duanwei: [],
    game_dw: '',
    game_xb: '',
    dw1: true,
    dw2: true,
    dw3: true,
    dw4: true,
    dw5: true,
    jietu: '',
    refereeid: '',
    name: '',
    game_homepage: '',
    wzsb_name: '',
    tmp_game_dw_id: 0
  },
  bindPickerChange1: function (e) {
    let _this = this
    _this.setData({
      index1: e.detail.value,
      dw1: false
    })
  },
  bindPickerChange2: function (e) {
    let _this = this
    _this.setData({
      index2: e.detail.value,
      dw2: false
    })
  },
  bindPickerChange3: function (e) {
    let _this = this
    _this.setData({
      index3: e.detail.value,
      dw3: false
    })
  },
  bindPickerChange4: function (e) {
    let _this = this
    _this.setData({
      index4: e.detail.value,
      dw4: false
    })
  },
  bindPickerChange5: function (e) {
    let _this = this
    _this.setData({
      index5: e.detail.value,
      dw5: false
    })
  },
  upload() {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        var tempFiles = res.tempFiles[0]['tempFilePath'];
        var fileSize = res.tempFiles[0]['size'];
        var MAX_SIZE = Number(that.data.peizhi.config_param39) * 1024 * 1024;
        if (fileSize > MAX_SIZE) {
          wx.showToast({
            title: '文件超过大小限制',
            icon: 'none',
          });
          // 文件超过限制，处理逻辑，比如重新选择
          return;
        } else {
          wx.uploadFile({
            url: app.apiurl + 'api/common/upload',
            filePath: tempFiles,
            name: 'file',
            formData: {},
            success(res) {
              const data = JSON.parse(res.data)
              that.setData({
                jietu: data.data.fullurl
              })
            }
          })
        }
      }
    })
  },
  z_upload() {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        var tempFiles = res.tempFiles[0]['tempFilePath'];
        var fileSize = res.tempFiles[0]['size'];
        var MAX_SIZE = Number(that.data.peizhi.config_param39) * 1024 * 1024;
        if (fileSize > MAX_SIZE) {
          wx.showToast({
            title: '文件超过大小限制',
            icon: 'none',
          });
          // 文件超过限制，处理逻辑，比如重新选择
          return;
        } else {
          wx.uploadFile({
            url: app.apiurl + 'api/common/upload',
            filePath: tempFiles,
            name: 'file',
            formData: {},
            success(res) {
              const data = JSON.parse(res.data)
              that.setData({
                game_homepage: data.data.fullurl
              })
            }
          })
        }
      }
    })
  },
  onChooseAvatar(e) {
    let that = this
    var tempFiles = e.detail.avatarUrl;
    wx.uploadFile({
      url: app.apiurl + 'api/common/upload',
      filePath: tempFiles,
      name: 'file',
      formData: {},
      success(res) {
        const data = JSON.parse(res.data)
        that.setData({
          jietu: data.data.fullurl
        })
      }
    })
  },
  z_nav1() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.z_jilu1()
    // this.gameleixing()
    this.gameduanwei()
    this.z_yonghu()
    this.z_peizhi()
  },
  z_peizhi() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/configBase',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          peizhi: res.data.data
        })
      }
    })
  },
  gameduanwei() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/gameDw?game_type_id=1',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        let array2 = _this.data.array2
        for (let i = 0; i < res.data.data.length; i++) {
          array2.push(res.data.data[i].name)
        }
        _this.setData({
          duanwei: res.data.data,
          array2: array2,
        })
      }
    })
  },
  z_yonghu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/myInfo',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          _this.setData({
            yonghu: res.data.data,
            jietu: res.data.data.head_img,
            name: res.data.data.name,
            game_homepage: res.data.data.game_homepage,
            wzsb_name: res.data.data.wzsb_name,
            tmp_game_dw_id: res.data.data.game_dw_id
          })
          if (res.data.data.refereeid != '0') {
            _this.setData({
              refereeid: res.data.data.refereeid,
            })
          }
          if (res.data.data.game_area > 0) {
            _this.setData({
              index1: _this.data.array1.indexOf(res.data.data.game_area_name),
              dw1: false
            })
          }
          if (res.data.data.game_dw_id > 0) {
            _this.setData({
              index2: _this.data.array2.indexOf(res.data.data.game_dw_name),
              dw2: false
            })
          }
          if (res.data.data.scfl_num > 0) {
            _this.setData({
              index4: _this.data.array4.indexOf(res.data.data.scfl_num_name),
              dw4: false
            })
          }
          if (res.data.data.phone_type > 0) {
            _this.setData({
              index5: _this.data.array5.indexOf(res.data.data.phone_type_name),
              dw5: false
            })
          }
          if (res.data.data.sex != '') {
            _this.setData({
              index3: _this.data.array3.indexOf(res.data.data.sex),
              dw3: false
            })
          }
        }
      }
    })
  },
  z_jilu1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/member/newUserGuide',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list4: res.data.data
        })
      }
    })
  },
  z_xiugai() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "name": '',
      "tel": '',
      "sex": '',
      "head_img": '',
      "lat": '',
      "lng": ''
    }
    wx.request({
      url: app.apiurl + 'api/member/genAdd',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  z_xiugai1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "head_img": _this.data.jietu,
      "game_name": _this.data.name,
      "game_area": Number(_this.data.index1) + 1,
      "game_dw": _this.data.duanwei[_this.data.index2].id,
      "sex": _this.data.array3[_this.data.index3],
      "pic": _this.data.game_homepage,
      "refereeid": _this.data.refereeid,
      "scfl_num": Number(_this.data.index4) + 1,
      "phone_type": Number(_this.data.index5) + 1,
      "wzsb_name": _this.data.wzsb_name,
    }
    if (_this.data.jietu == '') {
      wx.showToast({
        title: '请添加头像',
        icon: 'none',
        duration: 2000
      })
    } else if (_this.data.name == '') {
      wx.showToast({
        title: '请填写游戏昵称',
        icon: 'none',
        duration: 2000
      })
    } else if (_this.data.dw1) {
      wx.showToast({
        title: '请选择游戏区',
        icon: 'none',
        duration: 2000
      })
    } else if (_this.data.dw2) {
      wx.showToast({
        title: '请选择游戏段位',
        icon: 'none',
        duration: 2000
      })
    } else if (_this.data.dw3) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none',
        duration: 2000
      })
    } else if (_this.data.dw4) {
      wx.showToast({
        title: '请选择擅长分路',
        icon: 'none',
        duration: 2000
      })
    } else {
      if (_this.data.tmp_game_dw_id == '0') {
        wx.request({
          url: app.apiurl + 'api/member/genAdd',
          method: 'POST',
          data: params,
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 修改请求
          },
          success(res) {
            if (res.data.code == '1001') {
              wx.setStorageSync("xinrenyindao", '13')
              wx.setStorageSync("wanshanziliao1", '1')
              wx.reLaunch({
                url: '/pages/index/index',
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      } else {
        wx.request({
          url: app.apiurl + 'api/member/upGameLevel',
          method: 'POST',
          data: params,
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 修改请求
          },
          success(res) {
            if (res.data.code == '1001') {
              wx.showToast({
                title: '游戏资料已更新',
                icon: 'none',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    }
  },
  z_shuru1(e) {
    this.setData({
      name: e.detail.value
    })
  },
  z_shuru2(e) {
    this.setData({
      refereeid: e.detail.value
    })
  },
  z_shuru3(e) {
    this.setData({
      wzsb_name: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})